package com.university.mentalhealthplatform.global.redisGuard;

import com.university.mentalhealthplatform.appModels.Assessment;
import com.university.mentalhealthplatform.appModels.BookingLecture;
import com.university.mentalhealthplatform.appModels.Detail;
import com.university.mentalhealthplatform.appModels.Lecture;
import com.university.mentalhealthplatform.appModels.Message;
import com.university.mentalhealthplatform.appModels.Reply;
import com.university.mentalhealthplatform.appModels.Student;
import com.university.mentalhealthplatform.appModels.Test;
import com.university.mentalhealthplatform.appModels.Tweet;
import com.university.mentalhealthplatform.global.rbac.model.User;
import com.university.mentalhealthplatform.studentV3.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Service
public class DatabaseService {
    @Autowired
    private UserRepositoryV3 userRepositoryV3;

    @Autowired
    private AssessmentRepositoryV3 assessmentRepositoryV3;

    @Autowired
    private BookingLectureRepositoryV3 bookingLectureRepositoryV3;

    @Autowired
    private DetailRepositoryV3 detailRepositoryV3;

    @Autowired
    private LectureRepositoryV3 lectureRepositoryV3;

    @Autowired
    private MessageRepositoryV3 messageRepositoryV3;

    @Autowired
    private ReplyRepositoryV3 replyRepositoryV3;

    @Autowired
    private StudentRepositoryV3 studentRepositoryV3;

    @Autowired
    private TestRepositoryV3 testRepositoryV3;

    @Autowired
    private TweetRepository tweetRepository;

    public <T> T getDataFromDatabase(String key, Class<T> type) {
        if (type.equals(User.class)) {
            Optional<User> user = userRepositoryV3.findByUsername(key);
            return type.cast(user.orElse(null));
        } else if (type.equals(Assessment.class)) {
            Optional<Student> studentOpt = studentRepositoryV3.findById(UUID.fromString(key)); // 假设存在根据ID查找Student的方法
            if (studentOpt.isPresent()) {
                return type.cast(assessmentRepositoryV3.findByStudentId(studentOpt.get()));
            } else {
                return null; // 或者抛出异常，具体情况具体处理
            }
        } else if (type.equals(BookingLecture.class)) {
            return type.cast(bookingLectureRepositoryV3.findByPhone(key)); // 根据实际参数调整
        } else if (type.equals(Detail.class)) {
            return type.cast(detailRepositoryV3.findByType(key));
        } else if (type.equals(Lecture.class)) {
            return type.cast(lectureRepositoryV3.findById(Long.valueOf(key))); // 假设key是Lecture的ID
        } else if (type.equals(Message.class)) {
            return type.cast(messageRepositoryV3.findByStudentId(UUID.fromString(key))); // 假设key是UUID格式的学生ID
        } else if (type.equals(Reply.class)) {
            return type.cast(replyRepositoryV3.findById(Long.valueOf(key))); // 假设key是Reply的ID
        } else if (type.equals(Student.class)) {
            return type.cast(studentRepositoryV3.findById(UUID.fromString(key))); // 假设key是UUID格式的学生ID
        } else if (type.equals(Test.class)) {
            return type.cast(testRepositoryV3.findById(Integer.valueOf(key))); // 假设key是Test的ID
        } else if (type.equals(Tweet.class)) {
            return type.cast(tweetRepository.findById(Long.valueOf(key))); // 假设key是Tweet的ID
        }

        return null;
    }
}
